package cn.shujuhai.qtadmin.platform.domain;

import java.util.Collection;
import java.util.Date;
import java.util.List;

import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;

import lombok.*;

/**
 * 用户
 * 
 * @author dch798 TableName sys_user
 */
@TableName(value = "sys_user")
@EqualsAndHashCode(callSuper = true)
@Data
public class SysUser extends Model<SysUser> implements UserDetails {
    @TableField(exist = false)
    private static final long serialVersionUID = 1L;
    /**
     * 用户ID
     */
    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;
    /**
     * 姓名
     */
    @NotBlank
    @TableField(value = "username")
    private String username;
    /**
     * 性别
     */
    @NotBlank
    @TableField(value = "gender")
    private String gender;
    /**
     * 手机号码
     */
    @NotBlank
    @TableField(value = "mobile")
    private String mobile;
    /**
     * 邮箱
     */
    @Email
    @TableField(value = "email")
    private String email;
    /**
     * 职业
     */
    @NotBlank
    @TableField(value = "profession")
    private String profession;
    /**
     * 备注
     */
    @TableField(value = "remark")
    private String remark;
    /**
     * 头像图片地址
     */
    @TableField(value = "head_img_url")
    private String headImgUrl;
    /**
     * 真实姓名
     */
    @TableField(value = "realname")
    private String realname;
    /**
     * 最后登陆时间
     */
    @TableField(value = "last_login_time")
    private Date lastLoginTime;
    /**
     * 是否可用
     */
    @Getter(value = AccessLevel.NONE)
    @Setter(value = AccessLevel.NONE)
    @TableField(value = "enabled")
    private Boolean enabled;
    /**
     * 是否过期
     */
    @Getter(value = AccessLevel.NONE)
    @Setter(value = AccessLevel.NONE)
    @TableField(value = "account_non_expired")
    private Boolean accountNonExpired;
    /**
     * 是否锁定
     */
    @Getter(value = AccessLevel.NONE)
    @Setter(value = AccessLevel.NONE)
    @TableField(value = "account_non_locked")
    private Boolean accountNonLocked;
    /**
     * 证书是否过期
     */
    @Getter(value = AccessLevel.NONE)
    @Setter(value = AccessLevel.NONE)
    @TableField(value = "credentials_non_expired")
    private Boolean credentialsNonExpired;
    /**
     * 登录密码
     */
    @JsonIgnore
    @TableField(value = "password")
    private String password;
    /**
     * 密码加盐
     */
    @TableField(value = "salt")
    private String salt;

    /**
     * 创建时间
     */
    @TableField(value = "create_date", fill = FieldFill.INSERT)
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date creatDate;

    /**
     * 创建人
     */
    @TableField(value = "creator", fill = FieldFill.INSERT)
    private String creator;

    /**
     * 修改时间
     */
    @TableField(value = "edited", fill = FieldFill.INSERT_UPDATE)
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date edited;

    /**
     * 修改人
     */
    @TableField(value = "editor", fill = FieldFill.INSERT_UPDATE)
    private String editor;

    /**
     * 逻辑删除:0=未删除,1=已删除
     */
    @TableField(value = "deleted")
    private Boolean deleted;

    /**
     * 角色id
     */
    @TableField(exist = false)
    private Long[] roleIds;

    /**
     * 权限集合
     */
    @TableField(exist = false)
    private List<GrantedAuthority> authorities;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    @Override
    public String getUsername() {
        return email;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}